package com.chuangshu.chuangshuteam.controller;


import com.chuangshu.chuangshuteam.config.BaseContext;
import com.chuangshu.chuangshuteam.dto.Result;
import com.chuangshu.chuangshuteam.service.UploadFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/upload")
public class FileController {
    @Autowired
    private UploadFileService uploadFileService;
    @PostMapping("/file")
    public Result upload(MultipartFile file){
        log.info("用户上传文件 {}", file.getName());
        if (file==null||file.isEmpty()){
            return Result.fail("请选择文件");
        }
        Result upload = uploadFileService.upload(file);
        System.out.println(file.getContentType());

        return upload;

    }
    @PostMapping("/common/file")
    public Result uploadFiles(List<MultipartFile> files){
        log.info("用户上传文件");
        System.out.println("获取用户id"+BaseContext.getCurrentId());
        if (files==null||files.isEmpty()){
            return Result.fail("请选择文件");
        }
        Result upload = uploadFileService.uploadFiles(files);
        return upload;

    }
    @PostMapping("/common/file/url")
    public Result getUrl(MultipartFile files){
        log.info("用户上传文件");
       if (files==null||files.isEmpty()){
           return Result.fail("请选择文件");
       }
        String fileUrl = uploadFileService.getFileUrl(files);

       return Result.ok(fileUrl);

    }

    /**
     * 根据业务ID和业务类型查询文件列表
     * @param businessId 业务ID
     * @param businessType 业务类型
     * @return 文件列表
     */
    @GetMapping("/files")
    public Result getFileListByBusinessIdAndType(@RequestParam Long businessId, @RequestParam String businessType) {
        log.info("查询文件列表，businessId: {}, businessType: {}", businessId, businessType);
        return uploadFileService.getFileListByBusinessIdAndType(businessId, businessType);
    }

    /**
     * 根据业务ID和业务类型删除文件
     * @param businessId 业务ID
     * @param businessType 业务类型
     * @return 删除结果
     */
    @DeleteMapping("/files")
    public Result deleteFilesByBusinessIdAndType(@RequestParam Long businessId, @RequestParam String businessType) {
        log.info("删除文件，businessId: {}, businessType: {}", businessId, businessType);
        return uploadFileService.deleteFilesByBusinessIdAndType(businessId, businessType);
    }

    /**
     * 根据文件ID删除文件
     * @param fileId 文件ID
     * @return 删除结果
     */
    @DeleteMapping("/file/{fileId}")
    public Result deleteFileById(@PathVariable Long fileId) {
        log.info("根据ID删除文件，fileId: {}", fileId);
        return uploadFileService.deleteFileById(fileId);
    }

    /**
     * 根据文件ID查询文件详细信息
     * @param fileId 文件ID
     * @return 文件详细信息
     */
    @GetMapping("/file/{fileId}")
    public Result getFileById(@PathVariable Long fileId) {
        log.info("根据ID查询文件详细信息，fileId: {}", fileId);
        return uploadFileService.getFileById(fileId);
    }

}
